import upload from '@/utils/upload';
import request from '@/utils/request';

// 用户密码重置
export function updateUserPwd(oldPassword, newPassword) {
	const data = {
		oldPassword,
		newPassword,
	};
	return request({
		url: '/system/user/profile/updatePwd',
		method: 'put',
		params: data,
	});
}

// 查询用户个人信息
export function getUserProfile() {
	return request({
		url: '/system/user/profile',
		method: 'get',
	});
}

// 修改用户个人信息
export function updateUserProfile(data) {
	return request({
		url: '/system/user/profile',
		method: 'put',
		data: data,
	});
}

// /prod-api/system/user/12

// 用户头像上传
export function uploadAvatar(data) {
	return upload({
		url: '/system/user/profile/avatar',
		name: data.name,
		filePath: data.filePath,
	});
}

export function getDeptTree() {
	return request({
		url: '/system/dept/treeselect',
		method: 'get',
	});
}

export const getUserList = (data) => {
	return request({
		url: '/system/user/list',
		method: 'get',
		data: data,
	}).then((res) => {
		const { rows, total } = res;
		const result = [];
		rows?.forEach((item) => {
			const { dept, ...rest } = item;
			result.push({
				...rest,
				deptName: dept?.deptName,
			});
		});
		return Promise.resolve({
			rows: result,
			total
		});
	});
};

export function deleteUser(userId) {
	return request({
		url: `/system/user/${userId}`,
		method: 'delete',
	});
}

// 保存用户
export const addUser = (data) => {
	return request({
		url: `/system/user`,
		method: 'post',
		data,
	});
};

// 编辑用户
export const editUser = (data) => {
	return request({
		url: `/system/user`,
		method: 'put',
		data,
	});
};

// 获取岗位和角色
export const getPostAndRole = (userId = '') => {
	return request({
		url: `/system/user/${userId}`,
		method: 'get',
	});
};

export function getRoleList(data) {
	return request({
		url: '/system/role/list',
		method: 'get',
		data: data,
	});
}
export function deleteRole(roleId) {
	return request({
		url: `/system/role/${roleId}`,
		method: 'delete',
	});
}

// 保存用户
export const addRole = (data) => {
	return request({
		url: `/system/role`,
		method: 'post',
		data,
	});
};

// 编辑用户
export const editRole = (data) => {
	return request({
		url: `/system/role`,
		method: 'put',
		data,
	});
};

export const getMenuList = () => {
	return request({
		url: `/system/menu/treeselect`,
		method: 'get',
	});
}

export const getMenuListByRole = (roleId) => {
	return request({
		url: `/system/menu/roleMenuTreeselect/${roleId}`,
		method: 'get',
	});
}

export const getRoleMenuTreeselect = (roleIds) => {
	return request({
		url: `/system/menu/m/roleMenuTreeSelect`,
		method: 'get',
		data: { roleIds }
	});
}

export const getAllMenuTreeSelect = () => {
	return request({
		url: `/system/menu/m/allMenuTreeSelect`,
		method: 'get',
	});
}